import { createRouter, createWebHistory, RouteRecordRaw, RouteLocationNormalized } from 'vue-router'
import { h } from 'vue'
import PlatformCategoryManage from '@/views/product/PlatformCategoryManage.vue'

const Home = () => import('@/views/Home.vue')
const Login = () => import('@/views/Login.vue')
const MemberList = () => import('@/views/member/MemberList.vue')
const MemberLevel = () => import('@/views/member/MemberLevel.vue')
const MemberTag = () => import('@/views/member/MemberTag.vue')
const MemberData = () => import('@/views/dashboard/MemberData.vue')
const CopywritingData = () => import('../views/dashboard/CopywritingData.vue')
const NotFound = { template: '<div>404 Not Found</div>' }
const ProductList = () => import('@/views/product/ProductList.vue')
const RecommendManage = () => import('@/views/product/RecommendManage.vue')
const SessionManage = () => import('@/views/service/SessionManage.vue')
const QuickReplyManage = () => import('@/views/service/QuickReplyManage.vue')
const TagManage = () => import('@/views/service/TagManage.vue')
const TicketManage = () => import('@/views/service/TicketManage.vue')

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        component: Home,
        meta: {
            requiresAuth: true,
            requiresLayout: true
        }
    },
    {
        path: '/login',
        component: Login,
        meta: {
            isPublic: true,
            requiresLayout: false
        }
    },
    {
        path: '/profile',
        name: 'profile',
        component: () => import('@/views/Profile.vue'),
        meta: { title: '个人信息', requiresAuth: true }
    },
    {
        path: '/member',
        name: 'member',
        meta: { title: '会员管理', icon: 'User', requiresAuth: true },
        children: [
            {
                path: 'list',
                name: 'memberList',
                component: MemberList,
                meta: { title: '会员管理', requiresAuth: true }
            },
            {
                path: 'level',
                name: 'memberLevel',
                component: MemberLevel,
                meta: { title: '会员等级管理', requiresAuth: true }
            },
            {
                path: 'tag',
                name: 'memberTag',
                component: MemberTag,
                meta: { title: '会员标签管理', requiresAuth: true }
            },
            {
                path: 'payment/:id/:nickname',
                name: 'memberPayment',
                component: () => import('@/views/member/MemberPaymentDetail.vue'),
                meta: { title: '支付明细', requiresAuth: true }
            },
            {
                path: 'detail/:id/:nickname',
                name: 'memberDetail',
                component: () => import('@/views/member/MemberDetail.vue'),
                meta: { title: '会员详情', requiresAuth: true }
            }
            // 其他会员子路由可在此补充
        ]
    },
    {
        path: '/data-board',
        meta: { title: '数据看板', icon: 'DataLine', requiresAuth: true },
        redirect: '/data-board/goods',
        children: [
            {
                path: 'goods',
                name: 'dataGoods',
                component: () => import('@/views/dashboard/Workbench.vue'),
                meta: { title: '商品数据', requiresAuth: true }
            },
            {
                path: 'member-data',
                name: 'dataMember',
                component: MemberData,
                meta: { title: '会员数据', requiresAuth: true }
            },
            {
                path: 'copywriting-data',
                name: 'dataCopywriting',
                component: CopywritingData,
                meta: { title: '文案数据', requiresAuth: true }
            },
            {
                path: 'points-data',
                name: 'dataPoints',
                component: () => import('@/views/dashboard/PointsData.vue'),
                meta: { title: '积分数据', requiresAuth: true }
            },
            {
                path: 'coupon-data',
                name: 'dataCoupon',
                component: () => import('@/views/dashboard/CouponData.vue'),
                meta: { title: '优惠券数据', requiresAuth: true }
            },
            {
                path: 'warehouse-data',
                name: 'dataWarehouse',
                component: () => import('@/views/dashboard/WarehouseData.vue'),
                meta: { title: '仓储数据', requiresAuth: true }
            },
            {
                path: 'service-data',
                name: 'dataService',
                component: () => import('@/views/dashboard/ServiceData.vue'),
                meta: { title: '客服数据', requiresAuth: true }
            },
            {
                path: 'recommend',
                name: 'dataRecommend',
                component: () => import('@/views/dashboard/RecommendData.vue'),
                meta: { title: '商品推荐数据', requiresAuth: true }
            },
        ]
    },
    {
        path: '/product',
        meta: { title: '商品管理', icon: 'Box', requiresAuth: true },
        redirect: '/product/list',
        children: [
            {
                path: 'list',
                name: 'productList',
                component: ProductList,
                meta: { title: '商品管理', requiresAuth: true }
            },
            {
                path: 'audit',
                name: 'productAudit',
                component: () => import('@/views/product/ProductAudit.vue'),
                meta: { title: '商品审核', requiresAuth: true }
            },
            {
                path: 'category',
                name: 'productCategory',
                component: () => import('@/views/product/CategoryManage.vue'),
                meta: { title: '全站分类管理', requiresAuth: true }
            },
            {
                path: 'site-category',
                name: 'productSiteCategory',
                component: () => import('@/views/product/SiteCategoryManage.vue'),
                meta: { title: '分站分类管理', requiresAuth: true }
            },
            {
                path: 'tag',
                name: 'productTag',
                component: () => import('@/views/product/ProductTagManage.vue'),
                meta: { title: '商品标签管理', requiresAuth: true }
            },
            {
                path: 'recommend',
                name: 'productRecommend',
                component: RecommendManage,
                meta: { title: '商品推荐管理', requiresAuth: true }
            },
            {
                path: 'platform-mapping',
                name: 'productPlatformMapping',
                component: () => import('@/views/product/PlatformMapping.vue'),
                meta: { title: '平台映射管理', requiresAuth: true }
            },
            {
                path: 'platform-category',
                name: 'productPlatformCategory',
                component: PlatformCategoryManage,
                meta: { title: '平台分类管理', requiresAuth: true }
            }
        ]
    },
    {
        path: '/operation',
        meta: { title: '运营管理', icon: 'Promotion', requiresAuth: true },
        children: [
            {
                path: 'copywriting-manage',
                name: 'operationCopywritingManage',
                component: () => import('@/views/copywriting-manage.vue'),
                meta: { title: '文案管理', requiresAuth: true }
            },
            {
                path: 'points',
                meta: { title: '积分管理', requiresAuth: true },
                // redirect: '/operation/points/rule',
                children: [
                    { path: 'task', name: 'pointsTask', component: () => import('@/views/points/PointsTask.vue'), meta: { title: '日常任务管理', requiresAuth: true } },
                    { path: 'activity', name: 'pointsActivity', component: () => import('@/views/points/PointsActivity.vue'), meta: { title: '营销活动管理', requiresAuth: true } },
                    { path: 'config', name: 'pointsConfig', component: () => import('@/views/points/PointsConfig.vue'), meta: { title: '积分配置', requiresAuth: true } },
                    { path: 'log', name: 'pointsLog', component: () => import('@/views/points/PointsLog.vue'), meta: { title: '积分日志', requiresAuth: true } },
                    { path: 'signin-rule', name: 'pointsSigninRule', component: () => import('@/views/points/PointsSigninRule.vue'), meta: { title: '积分签到', requiresAuth: true } },
                    { path: 'task-record', name: 'pointsTaskRecord', component: () => import('@/views/points/PointsTaskRecord.vue'), meta: { title: '任务完成记录', requiresAuth: true } },
                    { path: 'order', name: 'pointsOrder', component: () => import('@/views/points/PointsOrder.vue'), meta: { title: '积分订单', requiresAuth: true } },
                    { path: 'goods', name: 'pointsGoods', component: () => import('@/views/points/PointsGoods.vue'), meta: { title: '积分商品', requiresAuth: true } },
                    { path: 'user-manage', name: 'pointsUserManage', component: () => import('@/views/points/UserPointsManage.vue'), meta: { title: '用户积分管理', requiresAuth: true } },
                    { path: 'user-detail/:userId', name: 'UserPointsDetail', component: () => import('@/views/points/UserPointsDetail.vue'), meta: { title: '用户积分明细', requiresAuth: true } },
                    { path: 'risk-control', name: 'pointsRiskControl', component: () => import('@/views/points/RiskControl.vue'), meta: { title: '风控管理', requiresAuth: true } },
                ]
            },
            {
                path: 'coupon',
                name: 'couponManage',
                component: () => import('@/views/operation/CouponManage.vue'),
                meta: { title: '优惠券管理', requiresAuth: true }
            },
            {
                path: 'coupon-receive',
                name: 'couponReceive',
                component: () => import('@/views/operation/CouponReceive.vue'),
                meta: { title: '优惠券领取情况', requiresAuth: true }
            },
            {
                path: 'home-design',
                name: 'homeDesign',
                component: () => import('@/views/operation/HomeDesign.vue'),
                meta: { title: '首页装修', requiresAuth: true }
            },
            {
                path: 'announcement-manage',
                name: 'operationAnnouncement',
                component: () => import('@/views/operation/AnnouncementManage.vue'),
                meta: { title: '系统公告', requiresAuth: true }
            },
            {
                path: 'banner',
                name: 'BannerManagement',
                component: () => import('@/views/operation/BannerManagement.vue')
            },
            {
                path: 'coupon-center',
                name: 'couponCenterManagement',
                component: () => import('@/views/operation/CouponCenterManagement.vue'),
                meta: { title: '领券中心管理', requiresAuth: true }
            },
            {
                path: 'activity',
                name: 'ActivityManage',
                component: () => import('@/views/operation/ActivityManage.vue'),
                meta: { title: '活动管理', requiresAuth: true }
            },
            {
                path: 'site-popup',
                name: 'SitePopupManage',
                component: () => import('@/views/operation/SitePopupManage.vue'),
                meta: { title: '站点弹窗管理', requiresAuth: true }
            },
            {
                path: '/operation/notification',
                name: 'NotificationManagement',
                component: () => import('@/views/operation/NotificationManagement.vue')
            },
            {
                path: '/notification',
                name: 'NotificationManage',
                component: () => import('@/views/notification/NotificationManage.vue'),
                meta: { title: '通知管理', requiresAuth: true }
            }
        ]
    },
    {
        path: '/log',
        name: 'operationLog',
        component: () => import('@/views/log/OperationLog.vue'),
        meta: { title: '操作日志', icon: 'DocumentChecked', requiresAuth: true }
    },
    {
        path: '/copywriting/:id',
        name: 'CopywritingDetail',
        component: () => import('../views/CopywritingDetail.vue'),
        meta: { layout: false }
    },
    {
        path: '/service',
        meta: { title: '客服中心', icon: 'Service', requiresAuth: true },
        children: [
            {
                path: 'session',
                name: 'serviceSession',
                component: SessionManage,
                meta: { title: '会话管理', requiresAuth: true }
            },
            {
                path: 'quick-reply',
                name: 'serviceQuickReply',
                component: QuickReplyManage,
                meta: { title: '快捷回复管理', requiresAuth: true }
            },
            {
                path: 'tag',
                name: 'serviceTag',
                component: TagManage,
                meta: { title: '会话标签管理', requiresAuth: true }
            },
            {
                path: 'ticket',
                name: 'serviceTicket',
                component: TicketManage,
                meta: { title: '工单管理', requiresAuth: true }
            },
            {
                path: 'ticket/:id',
                name: 'serviceTicketDetail',
                component: () => import('@/views/service/TicketDetail.vue'),
                meta: { title: '工单详情', requiresAuth: true }
            },
            {
                path: 'knowledge',
                name: 'serviceKnowledge',
                component: () => import('@/views/service/KnowledgeManage.vue'),
                meta: { title: '知识库管理', requiresAuth: true }
            },
            {
                path: "staff",
                name: "serviceStaff",
                component: () => import('@/views/service/StaffManage.vue'),
                meta: { title: '客服管理', requiresAuth: true }
            },
            {
                path: '/service/handover',
                name: 'ResignationHandover',
                component: () => import('@/views/service/ResignationHandover.vue')
            },
            {
                path: '/service/satisfaction',
                name: 'SatisfactionEvaluation',
                component: () => import('@/views/service/SatisfactionEvaluation.vue')
            },
            {
                path: '/service/scheduling',
                name: 'SchedulingManagement',
                component: () => import('@/views/service/SchedulingManagement.vue')
            },
            { path: '/service/performance', name: 'PerformanceStatistics', component: () => import('@/views/service/PerformanceStatistics.vue') },
            { path: '/service/robot', name: 'RobotService', component: () => import('@/views/service/RobotService.vue') },
        ]
    },
    {
        path: '/warehouse',
        meta: { title: '仓储中心', icon: 'OfficeBuilding', requiresAuth: true },
        children: [
            {
                path: 'location',
                name: 'warehouseLocation',
                component: () => import('@/views/warehouse/LocationManage.vue'),
                meta: { title: '库位管理', requiresAuth: true }
            },
            {
                path: 'inventory',
                name: 'warehouseInventory',
                component: () => import('@/views/warehouse/InventoryManage.vue'),
                meta: { title: '仓库盘点', requiresAuth: true }
            },
            // 新增独立维护页面路由
            {
                path: 'location-edit',
                name: 'warehouseLocationEdit',
                component: () => import('@/views/warehouse/LocationEdit.vue'),
                meta: { title: '新增/编辑库位', requiresAuth: true }
            },
            {
                path: 'warehouse-manage',
                name: 'warehouseWarehouseManage',
                component: () => import('@/views/warehouse/WarehouseManage.vue'),
                meta: { title: '仓库维护', requiresAuth: true }
            },
            {
                path: 'zone-manage',
                name: 'warehouseZoneManage',
                component: () => import('@/views/warehouse/ZoneManage.vue'),
                meta: { title: '分区维护', requiresAuth: true }
            },
            {
                path: 'shelf-manage',
                name: 'warehouseShelfManage',
                component: () => import('@/views/warehouse/ShelfManage.vue'),
                meta: { title: '货架维护', requiresAuth: true }
            },
            {
                path: 'type-manage',
                name: 'warehouseTypeManage',
                component: () => import('@/views/warehouse/TypeManage.vue'),
                meta: { title: '类型维护', requiresAuth: true }
            },
        ]
    },
    {
        path: '/finance',
        meta: { title: '财务管理', icon: 'WalletFilled', requiresAuth: true },
        children: [
            {
                path: 'voucher',
                name: 'financeVoucher',
                component: () => import('@/views/finance/VoucherManage.vue'),
                meta: { title: '财务凭证管理', requiresAuth: true }
            },
            {
                path: 'accounting-entry',
                name: 'financeAccountingEntry',
                component: () => import('@/views/finance/AccountingEntry.vue'),
                meta: { title: '会计分录', requiresAuth: true }
            },
            {
                path: 'audit',
                name: 'financeAudit',
                component: () => import('@/views/finance/FinanceAudit.vue'),
                meta: { title: '财务审核', requiresAuth: true }
            },


        ]
    },
    {
        path: '/report',
        component: () => import('@/views/report/ReportCenter.vue'),
        meta: { title: '报表中心', icon: 'PieChart', requiresAuth: true }
    },
    {
        path: '/settings',
        meta: { title: '基础设置', icon: 'Setting', requiresAuth: true },
        children: [
            { path: 'staff', name: 'staffManage', component: () => import('@/views/settings/StaffManage.vue'), meta: { title: '员工管理', requiresAuth: true } },
            { path: 'role', name: 'roleManage', component: () => import('@/views/settings/RoleManage.vue'), meta: { title: '角色管理', requiresAuth: true } },
            { path: 'site', name: 'siteManage', component: () => import('@/views/settings/SiteManage.vue'), meta: { title: '站点管理', requiresAuth: true } },
            { path: 'dictionary', name: 'dictionary', component: () => import('@/views/settings/Dictionary.vue'), meta: { title: '数据字典', requiresAuth: true } },
            { path: 'account', name: 'accountManage', component: () => import('@/views/settings/AccountManage.vue'), meta: { title: '账号管理', requiresAuth: true } },
            { path: 'bank-sites/:id', name: 'bankSitesDetail', component: () => import('@/views/settings/BankSitesDetail.vue'), meta: { title: '绑定站点详情', requiresAuth: true } },
            { path: 'trade-users/:id', name: 'tradeUsersDetail', component: () => import('@/views/settings/TradeUsersDetail.vue'), meta: { title: '绑定用户详情', requiresAuth: true } },
            { path: 'extra-service', name: 'extraService', component: () => import('@/views/settings/ExtraService.vue'), meta: { title: '收费管理', requiresAuth: true } },
            { path: 'additional-service', name: 'additionalService', component: () => import('@/views/settings/AdditionalService.vue'), meta: { title: '附加服务', requiresAuth: true } },
            { path: 'freight-rule', name: 'freightRule', component: () => import('@/views/settings/FreightRule.vue'), meta: { title: '国际物流费/运费规则', requiresAuth: true } },
            { path: 'freight-rule/ems/detail', name: 'emsFreightDetail', component: () => import('@/views/settings/EMSFreightDetail.vue'), meta: { title: 'EMS运费详情', requiresAuth: true } },
            { path: 'freight-rule/:id', name: 'freightRuleDetail', component: () => import('@/views/settings/FreightRuleDetail.vue'), meta: { title: '物流规则详情', requiresAuth: true } },
            { path: 'workflow', name: 'workflowList', component: () => import('@/views/settings/WorkflowList.vue'), meta: { title: '工作流配置', requiresAuth: true } },
            { path: 'workflow/new', name: 'workflowCreate', component: () => import('@/views/settings/WorkflowDesigner.vue'), meta: { title: '新建工作流', requiresAuth: true } },
            { path: 'workflow/:id', name: 'workflowEdit', component: () => import('@/views/settings/WorkflowDesigner.vue'), meta: { title: '编辑工作流', requiresAuth: true } },
            { path: 'seller', name: 'seller', component: () => import('@/views/settings/SellerManage.vue') },
            { path: 'package', name: 'package', component: () => import('@/views/settings/PackageSetting.vue') },
            { path: 'exchange', name: 'exchangeManage', component: () => import('@/views/settings/ExchangeManage.vue'), meta: { title: '汇率管理', requiresAuth: true } },
            { path: 'push-method', name: 'pushMethodManage', component: () => import('@/views/settings/PushMethodManage.vue'), meta: { title: '推送方式管理', requiresAuth: true } },
            { path: 'language', name: 'languageManage', component: () => import('@/views/settings/LanguageManage.vue'), meta: { title: '多语言管理', requiresAuth: true } }
        ]
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: NotFound,
        meta: { requiresLayout: false }
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized) => {
    return true
})

export default router
